package com.huike.web.controller.report;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.DataVo;
import com.huike.report.service.IReportService;
import com.huike.web.controller.vo.BaseDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    ITbClueService itbClueService;

    @Autowired
    TbBusinessMapper tbBusinessmapper;
    @Autowired
    IReportService reportService;
    @Autowired
    TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    TbContractMapper tbContractMapper;

    @GetMapping("/salesStatistic")
    public AjaxResult salesStatistic(IndexStatisticsVo request ){


        request.setBeginCreateTime(request.getBeginCreateTime()+" 00:00:00");
        request.setEndCreateTime(request.getEndCreateTime()+" 23:59:59");
        List<Map<String,Object>> list= reportService.salesStatistic(request);
        return AjaxResult.success(list);
    }

    @GetMapping
    public AjaxResult  getBaseData(String beginCreateTime , String endCreateTime,String deptId){
        String username = SecurityUtils.getUsername();
        List<TbAssignRecord> tbClues = itbClueService.selectAll(beginCreateTime, endCreateTime, username);
        BaseDataVo baseDataVo = new BaseDataVo();
         //	cluesNum：	  线索数  type = 0 ,tb_assign_record
         baseDataVo.setClueNum(tbClues.size());
          //	businessNum：商机数量 type = 1 ,tb_assign_record
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = tbBusinessTrackRecordMapper.selectAllBusiness(beginCreateTime, endCreateTime, username);
         baseDataVo.setClueNum(tbBusinessTrackRecords.size());
        //    contractNum：客户数量
        List<TbContract> tbContracts = tbContractMapper.countAllClient(beginCreateTime, endCreateTime, username);
        baseDataVo.setContractNum(tbContracts.size());
        //   salesAmount：销售金额
        double sale = 0;
        for (TbContract tbContract : tbContracts) {
            sale = sale + tbContract.getCoursePrice();
        }
        baseDataVo.setSalesAmount(sale);

        return AjaxResult.success(baseDataVo);
    }


}
